<script type="text/javascript">
	$(function(){
		
		$("input[name=add_debit]").live('click',function(){
			$(this).parent().parent().before('<tr><th>ชื่อบัญชี</th><td>' + $('.debit_ddl').html() + ' <a href="#" class="icon icon-delete"></span></a></td></tr>');
			//$(".number").priceFormat({prefix: "",thousandsSeparator: ","}); 
		})
		$("input[name=add_credit]").click(function(){
			$(this).parent().parent().before('<tr><th>ชื่อบัญชี</th><td>' + $('.credit_ddl').html() + ' <a href="#" class="icon icon-delete"></span></a></td></tr>');
			//$(".number").priceFormat({prefix: "",thousandsSeparator: ","}); 
		})
		
		$("#content table a[href=#]").live('click',function(){
			$(this).parent().parent().remove();
			$("input[name='price[dr][]'],input[name='price[cr][]']").trigger('keyup');
			return false;
		})
		
		$("a.delete").click(function(){
			if (confirm('ยืนยันการลบ')) {
				var a = $(this);
				var url = $(this).attr('href');
				$.post(url, function(data){
					a.parent().parent().remove();
				})
				$("input[name='price[dr][]'],input[name='price[cr][]']").trigger('keyup');
			}
			return false;
		})
		
		
		$("#frmMain").validate({
			rules: 
			{
				name: 
				{ 
					required: true
				}
			},
			messages:
			{
				name: 
				{ 
					required: "กรุณากรอกชื่อค่ะ"
				}
			}
		});
	})
</script>
<h1><?php echo $fund['name'] ?></h1>
<ul id="breadcrumb">
	<li><span>จัดการช้อมูลหลัก</span></li>	
	<li>จับคู่บัญชี</li>		
</ul>
<div id="content">
<form id="frmMain" action="voucher/match/save" method="post" >
<table class="form">
	<tr>
		<th>ประเภท</th>
		<td>
			<input type="radio" name="voucher_type_id" value="1" <?php echo (($rs['voucher_type_id']==1)||(!isset($rs['voucher_type_id'])))?'checked="checked"':'' ?> /><label>ด้านรับ</label>
			<input type="radio" name="voucher_type_id" value="2" <?php echo ($rs['voucher_type_id']==2)?'checked="checked"':'' ?> /><label>ด้านจ่าย</label>
			<input type="radio" name="voucher_type_id" value="3" <?php echo ($rs['voucher_type_id']==3)?'checked="checked"':'' ?> /><label>ด้านทั่วไป</label>
		</td>
	</tr>
	<tr><th>รายการการลงบัญชี</th><td><input type="text" name="name" value="<?php echo $rs['name'] ?>"  /></td></tr>
	<tr><th colspan="2" style="background:#B7CCED;">เดบิต</th></tr>
	<?php if(isset($debits)): ?>
	<?php foreach ($debits as $debit): ?>
	<tr>
		<th>ชื่อบัญชี</th>
		<td>
			<?php echo form_dropdown('account_id[dr][]',get_option('ID','NAME','ACCOUNTS',' FUND_ID = '.$fund_id),$debit['account_id']) ?>
			
			<input type="hidden" name="detail_id[dr][]" value="<?php echo $debit['id'] ?>" /> 
			<a href="voucher/match/detail_delete/<?php echo $debit['id'] ?>" class="icon icon-delete delete"></span></a>
		</td>
	</tr>
	<?php endforeach; ?>
	<?php else: ?>
	<tr>
		<th>ชื่อบัญชี</th>
		<td>
			<?php echo form_dropdown('account_id[dr][]',get_option('ID','NAME','ACCOUNTS',' FUND_ID = '.$fund_id)) ?> 
			<a href="#" class="icon icon-delete"></span></a>
		</td>
	</tr>
	<?php endif; ?>
	<tr><th colspan="2"><input type="button" name="add_debit" value="เพิ่มเดบิต" class="button" /></th></tr>
	<tr><th colspan="2" style="background:#EDBFB7;">เครดิต</th></tr>
	<?php if(isset($credits)): ?>
	<?php foreach ($credits as $credit): ?>
	<tr>
		<th>ชื่อบัญชี</th>
		<td>
			<?php echo form_dropdown('account_id[cr][]',get_option('ID','NAME','ACCOUNTS',' FUND_ID = '.$fund_id),$credit['account_id']) ?> 
			
			<input type="hidden" name="detail_id[cr][]" value="<?php echo $credit['id'] ?>" /> 
			<a href="voucher/match/detail_delete/<?php echo $credit['id'] ?>" class="icon icon-delete  delete"></span></a>
		</td>
	</tr>
	<?php endforeach; ?>
	<?php else: ?>
	<tr>
		<th>ชื่อบัญชี</th>
		<td>
			<?php echo form_dropdown('account_id[cr][]',get_option('ID','NAME','ACCOUNTS',' FUND_ID = '.$fund_id)) ?> 
			
			<a href="#" class="icon icon-delete"></span></a>
		</td>
	</tr>
	<?php endif; ?>
	<tr><th colspan="2"><input type="button" name="add_credit" value="เพิ่มเครดิต" class="button" /></th></tr>
	<tr>
		<th></th>
		<td>
			<input type="submit" class="button" value="<?php echo lang('btn_submit')?>" />
			<input type="button" class="button" value="<?php echo lang('btn_back')?>" onclick="window.location = '<?php echo site_url(); ?>voucher/match'" />
			<input type="hidden" name="id" value="<?php echo $rs['id'] ?>" />
			<input type="hidden" name="fund_id" value="<?php echo $fund_id ?>" />
		</td>
	</tr>
</table>
</form>
</div>

<div class="debit_ddl" style="display:none;">
<?php echo form_dropdown('account_id[dr][]',get_option('ID','NAME','ACCOUNTS',' FUND_ID = '.$fund_id)) ?>
</div>
<div class="credit_ddl" style="display:none;">
<?php echo form_dropdown('account_id[cr][]',get_option('ID','NAME','ACCOUNTS',' FUND_ID = '.$fund_id)) ?>
</div>
